<template>
  <div ref="chart" style="height: 100%;" />
</template>
<script>
import { Chart } from '@antv/g2';
import { getTicks } from '@/utils/chartScale.js';
import { numFormat } from '@/utils/filter.js';
export default {
/*
  data:     所要传输的数据
  x:        x轴变量名(有默认值)
  y:        y轴变量名(有默认值)
  s:        系列值
  color:    图表颜色
  text:     tooltip中的文字，数组格式
  sText:    s系列值顺序
*/
  name: 'CoalCuttingChart',
  props: {
    data: {
      type: Array,
      reqired: true
    },
    x: {
      type: String,
      default: 'x'
    },
    y: {
      type: String,
      default: 'y'
    },
    s: {
      type: String,
      default: 's'
    },
    color: {
      type: Array,
      required: true
    },
    text: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    data: {
      handler() {
        this.updateData();
      }
    }
  },
  mounted() {
    this.initChart();
    this.drawChart();
  },
  methods: {
    getTicks,
    initChart() {
      const ref = this.$refs.chart;
      this.chart = new Chart({
        container: ref,
        width: ref.clientWidth,
        height: ref.clientHeight
      });
    },
    drawChart() {
      const ratio = window.ratio || 1;
      const x = this.x;
      const y = this.y;
      this.chart.data(this.data);
      this.chart.scale(y, {
        min: 0,
        tickCount: 6,
        tickMethod: this.getTicks
      });
      this.chart.scale(x, {
        tickCount: 6
      });
      // 提示框
      this.chart.tooltip({
        shared: true,
        showCrosshairs: true,
        crosshairs: {
          line: {
            style: {
              stroke: '#00FDFF',
              lineDash: [3 * ratio, 3 * ratio],
              lineWidth: 1 * ratio
            }
          }
        },
        customItems: items => {
          return items;
        },
        domStyles: {
          'g2-tooltip-title': {
            fontSize: '.875rem',
            color: '#8D8D8D'
          },
          'g2-tooltip-list-item': {
            fontSize: 0,
            overflow: 'hidden'
          },
          'g2-tooltip-name': {
            fontSize: '.75rem',
            color: '#8D8D8D'
          },
          'g2-tooltip-value': {
            fontSize: '.75rem',
            color: 'rgba(0, 0, 0, 0.6)',
            fontWeight: '780'
          }
        }
      });
      // x轴
      this.chart.axis(x, {
        label: {
          style: {
            fill: '#ADADAD',
            fontSize: 12 * ratio,
            fontFamily: 'Helvetica-regular',
            lineHeight: 14 * ratio
          },
          offset: 5 * ratio
        },
        line: {
          style: {
            stroke: '#FFFFFF',
            strokeOpacity: 0.4,
            lineWidth: 1 * ratio
          }
        },
        tickLine: false
      });
      // y轴
      this.chart.axis(y, {
        label: {
          style: {
            fill: '#ADADAD',
            fontSize: 12 * ratio,
            fontFamily: 'Helvetica-regular'
          },
          formatter: (text) => {
            return numFormat(Number(text));
          },
          offset: 5 * ratio,
          autoHide: false
        },
        line: null,
        grid: null
      });
      this.chart.legend(false);
      this.chart
        .line()
        .position(`${x}*${y}`)
        .color(this.s, this.color)
        .animate({
          appear: {
            delay: 0,
            duration: 2000
          }
        });
      this.chart.render();
    },
    // 更新数据
    updateData() {
      this.chart.changeData(this.data);
    }
  }
};
</script>
